/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning Life in Transition Survey (LITS)

--------------------------------------------------------------------------------*/

*LITS 1

clear
use "$data_dir/raw/LITS/LITS 2006 data.dta"

drop country
clonevar country = countryname

replace country = proper(country)
replace country = "Bosnia and Herzegovina" if country=="Bosnia"
replace country = "Czech Republic" if country=="Czechrep"
replace country = "Macedonia, the Former Yugoslav Republic of" if country=="Fyrom"
replace country = "Moldova, Republic of" if country=="Moldova"
replace country = "Russian Federation" if country=="Russia"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "LITS"
gen wave = 1

gen weight = weight_2

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q303_2==4|q303_2==5
replace trust_govt = 0 if q303_2==1|q303_2==2|q303_2==3

gen trust_justice = 1 if q303_4==4|q303_4==5
replace trust_justice = 0 if q303_4==1|q303_4==2|q303_4==3

gen trust_police = 1 if q303_7==4|q303_7==5
replace trust_police = 0 if q303_7==1|q303_7==2|q303_7==3

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if q303_5==4|q303_5==5 
replace trust_parties = 0 if q303_5==1|q303_5==2|q303_5==3

gen trust_parliament = 1 if q303_3==4|q303_3==5 
replace trust_parties = 0 if q303_3==1|q303_3==2|q303_3==3

gen trust_others = 1 if q302_1==4|q302_1==5 
replace trust_others = 0 if q302_1==1|q302_1==2|q302_1==3

gen trust_military = 1 if q303_6==4|q303_6==5 
replace trust_military = 0 if q303_6==1|q303_6==2|q303_6==3

gen trust_religious_organizations = 1 if q303_12==4|q303_12==5 
replace trust_religious_organizations = 0 if q303_12==1|q303_12==2|q303_12==3

pca trust_govt trust_justice trust_police trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2006
clonevar age = age1
clonevar yob = q104_y1
replace yob = survey_year - age if yob==.

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if q102_1==1
replace sex = 2 if q102_1==0
replace sex = -99 if q102_1==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing  

gen religion = .
replace religion = 1 if q706==1
replace religion = 2 if q706==2
replace religion = 3 if q706==3
replace religion = 4 if q706==4
replace religion = 5 if q706==5
replace religion = 6 if q706==6
replace religion = -99 if q706==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q501==2
replace edu = 2 if q501==3
replace edu = 3 if q501==4|q501==5|q501==6
replace edu = 4 if q501==1
replace edu = -99 if q501==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 1 if q405a==1
replace occupation = 2 if q405a==2
replace occupation = 3 if q405a==3
replace occupation = 4 if q405a==4
replace occupation = 5 if q405a==5
replace occupation = 6 if q405a==6
replace occupation = 7 if q405a==7
replace occupation = 8 if q405a==8
replace occupation = 9 if q405a==9
replace occupation = 0 if q405a==10
replace occupation = -99 if q405a==98|q405a==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = -99

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

clonevar income = q211
replace income = -99 if q211==66|q211==.

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

egen marriage = rowtotal(q601b_1 q601b_2 q601b_3 q601b_4 q601b_5 q601b_6 q601b_7 q601b_8 q601b_9 q601b_10 q601b_11 q601b_12 q601b_13 q601b_14 q601b_15 q601b_16 q601b_17 q601b_18 q601b_19), missing
gen marital = 1 if marriage==1 //assume if married at some point since 1989, then they have marriage status
replace marital = 2 if marriage==0
replace marital = -99 if marriage==.
drop marriage

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if tablec==1|tablec==3
replace urban = 2 if tablec==2
replace urban = -99 if tablec==.|urban==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/lits1_clean.dta", replace

***************************************************************************************************************

*LITS 2

clear
use "$data_dir/raw/LITS/lits2.dta"

drop country
clonevar country = country_

replace country = strltrim(country)
replace country = strrtrim(country)

replace country = "Bosnia and Herzegovina" if country=="Bosnia"
replace country = "Macedonia, the Former Yugoslav Republic of" if country=="Macedonia"
replace country = "United Kingdom" if country=="Great Britain"
replace country = "Moldova, Republic of" if country=="Moldova"
replace country = "Russian Federation" if country=="Russia"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "LITS"
gen wave = 2

drop weight
gen weight = XCweight

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q303b==4|q303b==5 
replace trust_govt = 0 if q303b==1|q303b==2|q303b==3

gen trust_justice = 1 if q303f==4|q303f==5 
replace trust_justice = 0 if q303f==1|q303f==2|q303f==3

gen trust_police = 1 if q303i==4|q303i==5 
replace trust_police = 0 if q303i==1|q303i==2|q303i==3

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if q303g==4|q303g==5 
replace trust_parties = 0 if q303g==1|q303g==2|q303g==3

gen trust_parliament = 1 if q303e==4|q303e==5 
replace trust_parties = 0 if q303e==1|q303e==2|q303e==3

gen trust_others = 1 if q302==4|q302==5 
replace trust_others = 0 if q302==1|q302==2|q302==3

gen trust_military = 1 if q303h==4|q303h==5 
replace trust_military = 0 if q303h==1|q303h==2|q303h==3

gen trust_religious_organizations = 1 if q303n==4|q303n==5 
replace trust_religious_organizations = 0 if q303n==1|q303n==2|q303n==3

pca trust_govt trust_justice trust_police trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2010
clonevar age = respondentage
replace age = -99 if respondentage<18 & respondentage!=.
gen yob = survey_year-age if age!=-99

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if q102_1==1
replace sex = 2 if q102_1==2
replace sex = -99 if q102_1==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if q716==1
replace religion = 2 if q716==2
replace religion = 3 if q716==3
replace religion = 4 if q716==4|q716==5|q716==6
replace religion = 5 if q716==7
replace religion = 6 if q716==8
replace religion = -99 if q716==.|q716==-1|q716==-99

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q515==2
replace edu = 2 if q515==3|q515==4
replace edu = 3 if q515==5|q515==6|q515==7
replace edu = 4 if q515==1
replace edu = -99 if q515==-1|q515==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = .
replace occupation = 2 if q505_1==1
replace occupation = 2 if q505_1==2
replace occupation = 2 if q505_1==3
replace occupation = 2 if q505_1==4
replace occupation = 2 if q505_1==5
replace occupation = 2 if q505_1==6
replace occupation = 2 if q505_1==7
replace occupation = 2 if q505_1==8
replace occupation = 2 if q505_1==9
replace occupation = 2 if q505_1==10
replace occupation = 3 if q505_1==11
replace occupation = 3 if q505_1==12
replace occupation = 1 if q505_1==13
replace occupation = 1 if q505_1==14
replace occupation = 4 if q505_1==15
replace occupation = 4 if q505_1==16
replace occupation = 4 if q505_1==17
replace occupation = 1 if q505_1==18
replace occupation = 1 if q505_1==19
replace occupation = 3 if q505_1==20
replace occupation = 5 if q505_1==21
replace occupation = 6 if q505_1==22
replace occupation = 6 if q505_1==23
replace occupation = 6 if q505_1==24
replace occupation = 6 if q505_1==25
replace occupation = 6 if q505_1==26
replace occupation = 7 if q505_1==27
replace occupation = 8 if q505_1==28
replace occupation = 8 if q505_1==29
replace occupation = 8 if q505_1==30
replace occupation = 3 if q505_1==31
replace occupation = 8 if q505_1==32
replace occupation = 3 if q505_1==33
replace occupation = 8 if q505_1==34
replace occupation = 8 if q505_1==35
replace occupation = 8 if q505_1==36
replace occupation = 3 if q505_1==37
replace occupation = 7 if q505_1==38
replace occupation = 9 if q505_1==39
replace occupation = 9 if q505_1==40
replace occupation = 8 if q505_1==41
replace occupation = 7 if q505_1==42
replace occupation = 7 if q505_1==43
replace occupation = 8 if q505_1==44
replace occupation = 7 if q505_1==45
replace occupation = 7 if q505_1==46
replace occupation = 7 if q505_1==47
replace occupation = 7 if q505_1==48
replace occupation = 7 if q505_1==49
replace occupation = 7 if q505_1==50
replace occupation = 7 if q505_1==51
replace occupation = 7 if q505_1==52
replace occupation = 7 if q505_1==53
replace occupation = 8 if q505_1==54
replace occupation = 7 if q505_1==55
replace occupation = 8 if q505_1==56
replace occupation = 9 if q505_1==57
replace occupation = 9 if q505_1==58
replace occupation = 9 if q505_1==59
replace occupation = 9 if q505_1==60
replace occupation = 5 if q505_1==61
replace occupation = 5 if q505_1==62
replace occupation = 5 if q505_1==63
replace occupation = 9 if q505_1==64
replace occupation = 5 if q505_1==65
replace occupation = 9 if q505_1==66
replace occupation = 3 if q505_1==67
replace occupation = 3 if q505_1==68
replace occupation = 5 if q505_1==69
replace occupation = 3 if q505_1==70
replace occupation = -99 if q505_1==71|q505_1==72|q505_1==73
replace occupation = -99 if q405a==-90|q405a==-1|q405a==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = -99

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		   11   Band 11
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = .
replace marital = 1 if q701==2
replace marital = 2 if q701==1|q701==3|q701==4|q701==5
replace marital = -99 if q701==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/lits2_clean.dta", replace

***************************************************************************************************************

*LITS 3

clear
use "$data_dir/raw/LITS/LiTS III.dta"

replace country = "Bosnia and Herzegovina" if country=="Bosnia and Herz."
replace country = "Czech Republic" if country=="Czech Rep."
replace country = "Macedonia, the Former Yugoslav Republic of" if country=="FYR Macedonia"
replace country = "Kyrgyzstan" if country=="Kyrgyz Rep."
replace country = "Moldova, Republic of" if country=="Moldova"
replace country = "Russian Federation" if country=="Russia"
replace country = "Slovakia" if country=="Slovak Rep."

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "LITS"
gen wave = 3

gen weight = weight_one

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q404b==4|q404b==5 
replace trust_govt = 0 if q404b==1|q404b==2|q404b==3

gen trust_justice = 1 if q404f==4|q404f==5 
replace trust_justice = 0 if q404f==1|q404f==2|q404f==3

gen trust_police = 1 if q404i==4|q404i==5 
replace trust_police = 0 if q404i==1|q404i==2|q404i==3

gen trust_civil_service = .

gen trust_local_govt = q404d==4|q404d==5 
replace trust_local_govt = 0 if q404d==1|q404d==2|q404d==3

gen trust_parties = 1 if q404g==4|q404g==5 
replace trust_parties = 0 if q404g==1|q404g==2|q404g==3

gen trust_parliament = 1 if q404e==4|q404e==5 
replace trust_parties = 0 if q404e==1|q404e==2|q404e==3

gen trust_others = 1 if q403==4|q403==5 
replace trust_others = 0 if q403==1|q403==2|q403==3

gen trust_military = 1 if q404h==4|q404h==5 
replace trust_military = 0 if q404h==1|q404h==2|q404h==3

gen trust_religious_organizations = 1 if q404n==4|q404n==5 
replace trust_religious_organizations = 0 if q404n==1|q404n==2|q404n==3

pca trust_govt trust_justice trust_police trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = 2016
clonevar age = age_pr
replace age = -99 if age_pr==.
gen yob = survey_year-age if age!=-99

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if gender_pr==1
replace sex = 2 if gender_pr==2
replace sex = -99 if gender_pr==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if q922==1
replace religion = 2 if q922==2
replace religion = 3 if q922==3
replace religion = 4 if q922==4|q922==5|q922==6
replace religion = 5 if q922==7
replace religion = 6 if q922==8
replace religion = -99 if q922==.|q922==-1|q922==-99

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q109_1==2
replace edu = 2 if q109_1==3|q109_1==4|q109_1==5
replace edu = 3 if q109_1==6|q109_1==7|q109_1==8
replace edu = 4 if q109_1==1
replace edu = -99 if q109_1==-1|q109_1==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if PRq515==1|PRq515==4|PRq515==5
replace employment_status = 3 if PRq515==2|PRq515==3==6
replace employment_status = -99 if PRq515==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		   11   Band 11
*		  -99   Missing

clonevar income_raw = q223
replace income_raw = . if inlist(q223, -97, -99)

egen panelid = group(country)
gen income = .
forvalues i = 1/34 {
	xtile income_bracket`i' = income_raw if panelid==`i' & income_raw!=., nq(10)
	replace income = income_bracket`i' if income==.
}

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = .
replace marital = 1 if q107_1==2
replace marital = 2 if q107_1==1|q107_1==3|q107_1==4|q107_1==5
replace marital = -99 if q107_1==.

*urban (already a variable in LITS III so don't need to create)
*			1   Urban
*			2   Rural
*		  -99   Missing

replace urban = -99 if urban==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/lits3_clean.dta", replace


***************************************************************************************************************

*LITS 4

clear
use "$data_dir/raw/LITS/lits_iv.dta"

decode country, gen(country_name)
drop country
rename country_name country

replace country = "Bosnia and Herzegovina" if country=="Bosnia and Herz."
replace country = "Czech Republic" if country=="Czechia"
replace country = "Macedonia, the Former Yugoslav Republic of" if country=="North Macedonia"
replace country = "Kyrgyzstan" if country=="Kyrgyz Republic"
replace country = "Moldova, Republic of" if country=="Moldova"
replace country = "Russian Federation" if country=="Russia"
replace country = "Slovakia" if country=="Slovak Republic"
replace country = "Palestine, State of" if country=="West Bank & Gaza"
replace country = "Turkey" if country=="Türkiye"

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "LITS"
gen wave = 4

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if q403b==4|q403b==5 
replace trust_govt = 0 if q403b==1|q403b==2|q403b==3

gen trust_justice = 1 if q403f==4|q403f==5 
replace trust_justice = 0 if q403f==1|q403f==2|q403f==3

gen trust_police = 1 if q403i==4|q403i==5 
replace trust_police = 0 if q403i==1|q403i==2|q403i==3

gen trust_civil_service = .

gen trust_local_govt = q403d==4|q403d==5 
replace trust_local_govt = 0 if q403d==1|q403d==2|q403d==3

gen trust_parties = 1 if q403g==4|q403g==5 
replace trust_parties = 0 if q403g==1|q403g==2|q403g==3

gen trust_parliament = 1 if q403e==4|q403e==5 
replace trust_parties = 0 if q403e==1|q403e==2|q403e==3

gen trust_others = 1 if q402==4|q402==5 
replace trust_others = 0 if q402==1|q402==2|q402==3

gen trust_military = 1 if q403h==4|q403h==5 
replace trust_military = 0 if q403h==1|q403h==2|q403h==3

gen trust_religious_organizations = 1 if q403l==4|q403l==5 
replace trust_religious_organizations = 0 if q403l==1|q403l==2|q403l==3

pca trust_govt trust_justice trust_police trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = year(dofc(start_date))
clonevar age = q1051
replace age = -99 if q1051==.
gen yob = survey_year-age if age!=-99

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if q1031==1
replace sex = 2 if q1031==2
replace sex = -99 if q1031==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if q814==1
replace religion = 2 if q814==2
replace religion = 3 if q814==3
replace religion = 4 if q814==4|q814==5|q814==6
replace religion = 5 if q814==7
replace religion = 6 if q814==-95
replace religion = -99 if q814==.|q814==-99

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if q109b==2
replace edu = 2 if q109b==3|q109b==4|q109b==5
replace edu = 3 if q109b==6|q109b==7|q109b==8
replace edu = 4 if q109b==1
replace edu = -99 if q109b==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if q310==1|q310==4|q310==5
replace employment_status = 3 if q310==2|q310==3|q310==6
replace employment_status = -99 if q310==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		   11   Band 11
*		  -99   Missing

clonevar income_raw = q225
replace income_raw = . if inlist(q225, -97, -99)

egen panelid = group(country)
gen income = .
forvalues i = 1/37 {
	xtile income_bracket`i' = income_raw if panelid==`i' & income_raw!=., nq(10)
	replace income = income_bracket`i' if income==.
}

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = .
replace marital = 1 if q107b==2
replace marital = 2 if q107b==1|q107b==3|q107b==4|q107b==5
replace marital = -99 if q107b==.

*urban (already a variable in LITS III so don't need to create)
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = .
replace urban = 1 if urbanity==1
replace urban = 2 if urbanity==2
replace urban = -99 if urban==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = .

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/lits4_clean.dta", replace
